//
//  BIDStoreGoodsDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/17.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDStoreGoodsDetailViewController.h"
#import "BIDGoodsStandardListViewController.h"
#import "BIDStoreGoodsDetailCell.h"

#import <SDWebImage/UIImageView+WebCache.h>

static NSString *storeGoodDetailURL = @"commodityAndroid/getById.action";

@interface BIDStoreGoodsDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    //图片
    NSArray *_fileListArr;
    //规格
    NSArray *_standardArr;
}
@end

@implementation BIDStoreGoodsDetailViewController
@synthesize goodsID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"商品详情";
    _fileListArr = [[NSArray alloc] init];
    _standardArr = [[NSArray alloc] init];
    [_collectionView registerClass:[BIDStoreGoodsDetailCell class] forCellWithReuseIdentifier:@"identifier"];
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setItemSize:CGSizeMake(screenSize.width, screenSize.width)];
    [flowLayout setMinimumLineSpacing:0];
    [flowLayout setMinimumInteritemSpacing:0];
    [_collectionView setCollectionViewLayout:flowLayout];
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], storeGoodDetailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"commodity.id=%@", goodsID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self prepareLayoutWithDic:responseDic];
        }
    }];
}

- (void)prepareLayoutWithDic:(NSDictionary *)dic
{
    _fileListArr = [[NSArray alloc] initWithArray:dic[@"fileList"]];
    [_pageCtrl setNumberOfPages:_fileListArr.count];
    _standardArr = [[NSArray alloc] initWithArray:dic[@"specList"]];
    //标题
    _titleLabel.text = dic[@"name"];
    CGFloat height = [BIDGlobalMethods getHeightWithString:dic[@"name"] font:_titleLabel.font constraintSize:CGSizeMake(CGRectGetWidth(_titleLabel.frame), MAXFLOAT)];
    _heightForTitleLabelConstraint.constant = height+20;
    //价格
    if(dic[@"minPrice"] && dic[@"maxPrice"])
    {
        NSString *strMinPrice = ANY2NSSTRING(dic[@"minPrice"]);
        NSString *strMaxPrice = ANY2NSSTRING(dic[@"maxPrice"]);
        if([strMinPrice isEqualToString:strMaxPrice])
        {
            _priceLabel.text = [[NSString alloc] initWithFormat:@"￥%@", strMinPrice];
        }
        else
        {
            _priceLabel.text = [[NSString alloc] initWithFormat:@"￥%@ - ￥%@", strMinPrice, strMaxPrice];
        }
    }
    else
    {
        _priceLabel.text = @"";
    }
    //商品描述
    _descriptionLabel.text = dic[@"content"];
    height = [BIDGlobalMethods getHeightWithString:dic[@"content"] font:_descriptionLabel.font constraintSize:CGSizeMake(CGRectGetWidth(_descriptionLabel.frame), MAXFLOAT)];
    _heightForDescriptionLabelConstraint.constant = height;
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    _heightForContainerViewConstraint.constant = CGRectGetMaxY(_descriptionLabel.frame)+50;
    //CGSize contentSize = _scrollView.contentSize;
    //contentSize.height = CGRectGetHeight(_descriptionLabel.frame)+550;

    [_collectionView reloadData];
}

/**
 显示商品规格

 @param sender <#sender description#>
 */
- (IBAction)showGoodsStandard:(id)sender
{
    BIDGoodsStandardListViewController *vc = [[BIDGoodsStandardListViewController alloc] init];
    vc.dataSourceArr = [[NSArray alloc] initWithArray:_standardArr];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _fileListArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDStoreGoodsDetailCell *goodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = _fileListArr[row];
    NSString *strFilePath = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"path"]];
    [goodsCell.imgView setImageWithURL:[NSURL URLWithString:strFilePath] placeholderImage:[UIImage imageNamed:PIC_PLACEHOLDER]];

    cell = goodsCell;
    [cell.contentView setBackgroundColor:[UIColor whiteColor]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld", indexPath.row);
    NSUInteger row = indexPath.row;
}

@end
